package com.ktg.web;


import com.ktg.ktgcommon.domain.TmToolType;
import com.ktg.ktgcommon.result.R;
import com.ktg.ktgcommon.utils.Page;
import com.ktg.service.TmToolTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class TmToolTypeController {
    @Autowired
    private TmToolTypeService service;

    @RequestMapping("/typePage")
    public Page<TmToolType> page(TmToolType entity,
                                 @RequestParam(value = "pageNo" , defaultValue = "1")Integer pageNo,
                                 @RequestParam(value = "pageSize" , defaultValue = "5")Integer pageSize){
        Page<TmToolType> page = service.findPage(entity, pageNo, pageSize);
        return page;
    }

    @RequestMapping("/maintenTypeList")
    public List<TmToolType> maintenTypeList(){
        List<TmToolType> list = service.maintenTypeList();
        return list;
    }

    @PostMapping("/save")
    public Map save(TmToolType entity){
        Map map = new HashMap();
        boolean ok = false;
        if(entity.getToolTypeId() == null){
            ok = service.add(entity);
        }else{
            ok = service.update(entity);
        }
        if(ok){
            map.put("code",0);
        }else{
            map.put("code",1);
        }
        return map;
    }

    @PostMapping("/detail")
    public Map detail(@RequestBody Map params){
        Map map = new HashMap();
        Object toolTypeId = params.get("toolTypeId");

        if(toolTypeId != null){
            long ttid = Long.parseLong(toolTypeId.toString());
            TmToolType entity = service.findById(ttid);
            map.put("code", 0);
            map.put("data", entity);
        }else{
            map.put("code", 1);
        }
        return map;
    }

    @DeleteMapping("/del")
    public R delete(Long toolTypeId){
        int ok = service.delete(toolTypeId);
        if(ok > 0){
            return R.OK();
        }else{
            return R.FAIL();
        }
    }
}
